package wnhz.com.business.shareshop.fragment;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.base.BaseFragment;
import wnhz.com.business.base.BaseRecyclerAdapter;
import wnhz.com.business.groupshop.bean.OrderBean;
import wnhz.com.business.shareshop.adapter.FragmentAllAdapter;
import wnhz.com.business.shareshop.adapter.FragmentSixAdapter;
import wnhz.com.business.uitls.MyUtils;
import wnhz.com.business.uitls.decoration.HorizDecoration;
import wnhz.com.business.uitls.dialog.SimpleDialog;

import static android.app.Activity.RESULT_OK;

/**
 * Created by Administrator on 2018/1/27 0027.
 */

public class OrderSixFragment extends BaseFragment implements BaseRecyclerAdapter.OnDataLoadFinish, OnRefreshLoadmoreListener{

    @BindView(R.id.order_fragment_refreshLayout)
    SmartRefreshLayout order_fragment_refreshLayout;
    @BindView(R.id.recycler_View)
    RecyclerView recyclerView;
    @BindView(R.id.empty_layout)
    LinearLayout layEmtpy;
    @BindView(R.id.tv_msg)
    TextView tv_msg;

    private BaseActivity activity;
    private SimpleDialog simpleDialog;
    private Dialog call_up_dialog;
    private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 10086;
    private final int REQUEST_EVALUATE = 222;

    private FragmentSixAdapter fragmentSixAdapter;
    private List<OrderBean.InfoBean> orderInfos = new ArrayList<>();
    private int paging;

    public void setPaging(int paging) {
        this.paging = paging;
    }

    public static OrderFifthFragment getInstance() {
        OrderFifthFragment sf = new OrderFifthFragment();
        return sf;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootview = inflater.inflate(R.layout.order_one_fragment, null);
        ButterKnife.bind(this, rootview);
        activity = (BaseActivity) getActivity();

        iniRefreshLayout();
        return rootview;
    }

    private void iniRefreshLayout() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new HorizDecoration(20));

        order_fragment_refreshLayout.setOnRefreshLoadmoreListener(this);
        order_fragment_refreshLayout.setRefreshHeader(new ClassicsHeader(activity));
        order_fragment_refreshLayout.setRefreshFooter(new ClassicsFooter(activity));

        fragmentSixAdapter = new FragmentSixAdapter(activity, orderInfos);
        initView();
    }

    private void initView() {
        orderInfos.clear();
        fragmentSixAdapter.setOnDataLoadFinish(this);
        fragmentSixAdapter.loadDataFirstTime();
        recyclerView.setAdapter(fragmentSixAdapter);

        fragmentSixAdapter.setOnClickListener(new FragmentSixAdapter.MyItemClickLinstener() {
            @Override
            public void OnCustomerServiceClickListener(FragmentSixAdapter fragmentFifthAdapter, int position, View v) {
                simpleDialog = new SimpleDialog(getContext(), "是否联系客服?", "再想想", "确定", new SimpleDialog.OnButtonClick() {
                    @Override
                    public void onNegBtnClick() { //取消
                        simpleDialog.cancel();
                    }

                    @Override
                    public void onPosBtnClick() { //确认
                        final String phone = "";
                        if (!TextUtils.isEmpty(phone)) {
                            if (call_up_dialog == null) {
                                call_up_dialog = MyUtils.callUpdialDialog(activity, phone, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                                                && ContextCompat.checkSelfPermission(activity,
                                                Manifest.permission.CALL_PHONE)
                                                != PackageManager.PERMISSION_GRANTED) {
                                            ActivityCompat.requestPermissions(activity,
                                                    new String[]{Manifest.permission.CALL_PHONE},
                                                    MY_PERMISSIONS_REQUEST_CALL_PHONE);
                                        } else {
                                            Intent intent2 = new Intent(Intent.ACTION_DIAL);
                                            Uri data = Uri.parse("tel:" + phone);
                                            intent2.setData(data);
                                            startActivity(intent2);
                                        }
                                    }
                                });
                            } else {
                                call_up_dialog.show();
                            }
                        }
                    }
                });
                simpleDialog.show();
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_EVALUATE)
            if (resultCode == RESULT_OK) {
                //处理扫描结果（在界面上显示）
                if (null != data) {
                    fragmentSixAdapter.loadDataFirstTime();
                }
                super.onActivityResult(requestCode, resultCode, data);
            }
    }

    @Override
    public void loadDataFinished(int datasLength) {
        if (datasLength == 0) {
            layEmtpy.setVisibility(View.VISIBLE);
            tv_msg.setText("暂无订单~");
            recyclerView.setVisibility(View.GONE);
        } else {
            layEmtpy.setVisibility(View.GONE);
            if (paging == 0){
                order_fragment_refreshLayout.finishRefresh();
                order_fragment_refreshLayout.resetNoMoreData();
            }else {
                order_fragment_refreshLayout.finishLoadmore();
                fragmentSixAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                paging = paging + 1;
                initView();
            }
        },2000);
    }
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                paging = 0;
                initView();
            }
        },2000);
    }
}
